<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
        <a-form :form="form">
            <a-form-item label="采购方总部编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <j-dict-select-tag v-decorator="[ 'purchaserHqCode', validatorRules.purchaserHqCode, {}]" :triggerChange="true" placeholder="请选择采购方总部" dictCode="purchaserHqCode"/>
            </a-form-item>
            <a-form-item label="供应商名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <j-dict-select-tag @change="handleInput" v-decorator="[ 'supplierCode', validatorRules.supplierCode, {}]" :triggerChange="true" placeholder="请选择供应商名称" dictCode="supplierCode" />
                <a-input v-model="model.supplierName" type="hidden"/>
            </a-form-item>
            <a-form-item label="供应商工单编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <a-input v-decorator="[ 'supplierWorkNo', validatorRules.supplierWorkNo, {}]" placeholder="请输入供应商工单编号"></a-input>
            </a-form-item>
            <a-form-item label="采集规范版本号" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <a-input v-decorator="[ 'standardVersion', validatorRules.standardVersion, {}]" disabled></a-input>
            </a-form-item>
            <a-form-item label="规格型号编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <j-dict-select-tag v-decorator="[ 'modelCode', validatorRules.modelCode, {}]" :triggerChange="true" placeholder="请选择规格型号编码" dictCode="modelCode"/>
            </a-form-item>
            <a-form-item label="物资品类类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <j-dict-select-tag v-decorator="[ 'categoryType', validatorRules.categoryType]" :triggerChange="true" placeholder="请选择物资品类类型" dictCode="categoryType"/>
            </a-form-item>
            <a-form-item label="采集时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <j-date placeholder="请选择采集时间" showTime="true" dateFormat="YYYY-MM-DD HH:mm:ss" v-decorator="[ 'checkTime', validatorRules.checkTime]" :trigger-change="true" style="width: 100%"/>
            </a-form-item>
            <a-form-item label="入数采中心时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <j-date placeholder="请选择入数采中心时间" showTime="true" dateFormat="YYYY-MM-DD HH:mm:ss" v-decorator="[ 'putCenterTime', validatorRules.putCenterTime]" :trigger-change="true" style="width: 100%"/>
            </a-form-item>
            <a-form-item label="工序" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <a-input v-decorator="[ 'pdCode', validatorRules.pdCode]" placeholder="请输入工序"></a-input>
            </a-form-item>
            <a-form-item label="感知过程" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <a-input v-decorator="[ 'processType', validatorRules.processType,{}]" disabled></a-input>
            </a-form-item>
            <a-form-item label="(抗扭)检验结果是否合格" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <j-dict-select-tag v-decorator="[ 'torsionalStrength', validatorRules.torsionalStrength,{}]" :triggerChange="true" placeholder="请选择(抗扭)检验结果是否合格" dictCode="isQualify"/>
            </a-form-item>
            <a-form-item label="工频额定值" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <a-input-number v-decorator="[ 'industrialFrequencyUn', validatorRules.industrialFrequencyUn]" placeholder="请输入工频额定值" style="width: 100%"/>
            </a-form-item>
            <a-form-item label="工频值" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <a-input-number v-decorator="[ 'industrialFrequency', validatorRules.industrialFrequency]" placeholder="请输入工频值" style="width: 100%"/>
            </a-form-item>
            <a-form-item label="额定局放值" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <a-input-number v-decorator="[ 'partialDischargeUn', validatorRules.partialDischargeUn]" placeholder="请输入额定局放值" style="width: 100%"/>
            </a-form-item>
            <a-form-item label="局放值" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <a-input-number v-decorator="[ 'partialDischarge', validatorRules.partialDischarge]" placeholder="请输入局放值" style="width: 100%"/>
            </a-form-item>
            <a-form-item label="(绝缘操作杆(扭杆))检验结果是否合格" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <j-dict-select-tag v-decorator="[ 'isQualify', validatorRules.isQualify, {}]" :triggerChange="true" placeholder="请选择(绝缘操作杆(扭杆))检验结果是否合格" dictCode="isQualify"/>
            </a-form-item>
<!--            <a-form-item label="(绝缘操作杆(扭杆))检查结果附件文件名" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
<!--                <a-input v-decorator="[ 'checkResult', validatorRules.checkResult]" placeholder="请输入(绝缘操作杆(扭杆))检查结果附件文件名"></a-input>-->
<!--            </a-form-item>-->
            <a-form-item label="(绝缘操作杆(扭杆))检查结果附件对象" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <a-upload
                        name="avatar"
                        :multiple="true"
                        :data="token"
                        :headers="headers"
                        :default-file-list="defaultFileList"
                        :action="'/jeecg-boot' + '/fileupload/checkResultObj'"
                        :before-upload="beforeUpload"
                        @change="handleChange"
                >
                    <a-button> <a-icon type="upload" />上传图片</a-button>
                </a-upload>
                <a-input v-model="model.checkResult" type="hidden"/>
                <a-input v-model="model.checkResultObj" type="hidden"/>
                <a-input v-decorator="[ 'ioflag', validatorRules.ioflag]" type="hidden"/>
            </a-form-item>
        </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

    import {httpAction} from '@/api/manage'
    import pick from 'lodash.pick'
    import JDate from '@/components/jeecg/JDate'

    import { ACCESS_TOKEN } from "@/store/mutation-types"
    import Vue from 'vue'
    import {initDictOptions, filterDictText} from '@/components/dict/JDictSelectUtil'

    export default {
        name: "EipInsulationTorsionModal",
        components: {
            JDate,
        },
        data() {
            return {
                headers: {},
                token:{},
                form: this.$form.createForm(this),
                title: "操作",
                width: 800,
                visible: false,
                model: { },
                labelCol: {
                    xs: {span: 24},
                    sm: {span: 5},
                },
                wrapperCol: {
                    xs: {span: 24},
                    sm: {span: 16},
                },
                confirmLoading: false,
                defaultFileList:[],
                supplierCodeDictOptions:[],
                validatorRules: {
                puid: {},
                purchaserHqCode: {rules: [{required: true, message: '请输入采购方总部编码!'}]},
                supplierCode: {rules: [{required: true, message: '请输入侧供应商编码!'}]},
                supplierName: {rules: [{required: true, message: '请输入供应商名称!'}]},
                supplierWorkNo: {rules: [{required: true, message: '请输入供应商工单编号!'}]},
                standardVersion: {rules: [{required: true, message: '采集规范版本号!'}],initialValue:'1'},
                modelCode: {rules: [{required: true, message: '请输入规格型号编码!'}]},
                categoryType: {rules: [{required: true, message: '请输入物资品类类型!'}]},
                checkTime: {rules: [{required: true, message: '请输入采集时间!'}]},
                putCenterTime: {rules: [{required: true, message: '请输入入数采中心时间!'}]},
                pdCode: {rules: [{required: true, message: '请输入工序!'}]},
                processType: {rules: [{required: true, message: '感知过程!'}],initialValue:'1'},
                torsionalStrength: {},
                industrialFrequencyUn:{rules: [{required: true, message: '请输入工频额定值!'}]},
                industrialFrequency:{rules: [{required: true, message: '请输入工频值!'}]},
                partialDischargeUn:{rules: [{required: true, message: '请输入额定局放值!'}]},
                partialDischarge:{rules: [{required: true, message: '请输入局放值!'}]},
                isQualify: {},
                checkResult: {},
                checkResultObj: {},
                ioflag: {rules: [{required: true, message: '请输入状态表示!'}],initialValue:'1'},
                message: {},
                remark1: {},
                remark2: {},
                remark3: {},
                remark4: {},
                remark5: {},
                },
                url: {
                  add: "/eipInsulationTorsion/add",
                  edit: "/eipInsulationTorsion/edit",
                }
            }
        },
        created() {
            const token=Vue.ls.get(ACCESS_TOKEN);
            this.headers={ authorization: 'authorization-text',"X-Access-Token":token }
        },
        methods: {
            handleInput(e){
                this.model.supplierName = filterDictText(this.supplierCodeDictOptions, e);
            },
            handleChange(info) {
                if (info.file.status !== 'uploading') {
                    this.model.checkResult=info.file.name;
                    this.model.checkResultObj=info.file.response;
                }
                if (info.file.status === 'done') {

                    this.$message.success(`${info.file.name} file uploaded successfully`);
                } else if (info.file.status === 'error') {
                    this.$message.error(`${info.file.name} file upload failed.`);
                }

            },
            beforeUpload(file) {
                const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
                if (!isJpgOrPng) {
                    this.$message.error('You can only upload JPG file!');
                }
                const isLt2M = file.size / 1024 / 1024 < 2;
                if (!isLt2M) {
                    this.$message.error('Image must smaller than 2MB!');
                }
                return isJpgOrPng && isLt2M;
            },
            add() {
                this.defaultFileList=[];
                this.edit({});
            },
            edit(record) {
                this.form.resetFields();
                this.model = Object.assign({}, record);
                this.visible = true;
                this.$nextTick(() => {
                    this.form.setFieldsValue(pick(this.model, "puid", "purchaserHqCode", "supplierCode", "supplierName", "supplierWorkNo", "standardVersion", "modelCode", "categoryType", "checkTime", "putCenterTime", "pdCode", "processType", "torsionalStrength", "industrialFrequencyUn", "industrialFrequency", "partialDischargeUn", "partialDischarge", "isQualify", "checkResult", "checkResultObj", "ioflag", "message", "remark1", "remark2", "remark3", "remark4", "remark5"))
                })
            },
            close() {
                this.$emit('close');
                this.visible = false;
            },
            handleOk() {
                const that = this;
                // 触发表单验证
                this.form.validateFields((err, values) => {
                    if (!err) {
                        that.confirmLoading = true;
                        let httpurl = '';
                        let method = '';
                        if (!this.model.puid) {
                            httpurl += this.url.add;
                            method = 'post';
                        } else {
                            httpurl += this.url.edit;
                            method = 'put';
                        }
                        let formData = Object.assign(this.model, values);
                        console.log("表单提交数据", formData)
                        httpAction(httpurl, formData, method).then((res) => {
                            if (res.success) {
                                that.$message.success(res.message);
                                that.$emit('ok');
                            } else {
                                that.$message.warning(res.message);
                            }
                        }).finally(() => {
                            that.confirmLoading = false;
                            that.close();
                        })
                    }

                })
            },
            handleCancel() {
                this.close()
            },
            popupCallback(row) {
                this.form.setFieldsValue(pick(row, "puid", "purchaserHqCode", "supplierCode", "supplierName", "supplierWorkNo", "standardVersion", "modelCode", "categoryType", "checkTime", "putCenterTime", "pdCode", "processType", "torsionalStrength", "industrialFrequencyUn", "industrialFrequency", "partialDischargeUn", "partialDischarge", "isQualify", "checkResult", "checkResultObj", "ioflag", "message", "remark1", "remark2", "remark3", "remark4", "remark5"))
            }
        },
        mounted() {
            initDictOptions('supplierCode').then((res) => {
                if (res.success) {
                    this.supplierCodeDictOptions = res.result
                }
            });
        }
    }
</script>